package cn.codeforfun.common.interceptor;

import cn.codeforfun.common.model.User;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.PropKit;


public class AuthInterceptor implements Interceptor {
    public void intercept(Invocation inv) {
        //拦截开始
        String viewPath = inv.getActionKey();
        String[] exclude = PropKit.get("exclude") == null ? null : PropKit.get("exclude").trim().split(",");
        boolean flag = false;
        if (exclude != null && !"".equals(exclude)) {
            for (String param : exclude) {
                if (viewPath.equals(param)) {
                    flag = true;
                }
            }
        }
        if (flag) {
            inv.invoke();
        } else {
            User user = (User) inv.getController().getSession().getAttribute("user");
            if (user == null) {
                inv.getController().redirect("/loginPage");
            } else {
                inv.invoke();
            }
        }
    }
}
